/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.File;
import java.io.IOException;

import edu.pku.sei.modeler.jet.util.PathDescriptor;

/**
 * @author jesse
 *
 */
public class FileOperation {

	public static void deleteDirectory(File dir) throws IOException {
		if( (dir == null) || !dir.isDirectory()) {
			throw new IllegalArgumentException(
					"Argument "+dir+" is not a directory. "
					);
		  	}
		  	File[ ] entries = dir.listFiles( );
		  	int sz = entries.length;
		  	for(int i=0; i<sz; i++) {
		  		if(entries[i].isDirectory( )) {
		  			deleteDirectory(entries[i]);
		  		} else {
		  			entries[i].delete( );
		  		}
		  	}
		  	dir.delete();
	}
	
	public void DeleteRuntimeDir() {
		PathDescriptor p = new PathDescriptor();
		String path = p.getRuntimeAbsolutePath();
		File f = new File(path);
		if(f.exists())
		try {
			deleteDirectory(f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void DeleteJETEmitterDir() {
		PathDescriptor p = new PathDescriptor();
		String path = p.getRuntimeAbsolutePath()+".JETEmitters/";
		File f = new File(path);
		if(f.exists())
		try {
			deleteDirectory(f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void DeleteProjectNameDir(String name) {
		PathDescriptor p = new PathDescriptor();
		String path = p.getRuntimeAbsolutePath()+name+"/";
		File f = new File(path);
		if(f.exists())
		try {
			deleteDirectory(f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void CleanDir(String name){
		DeleteJETEmitterDir();
		DeleteProjectNameDir(name);
	}
}
